package com.ruizhong.PIW.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.PIW.dao.NavBtnMapper;
import com.ruizhong.PIW.entity.TNavbtn;
import com.ruizhong.PIW.service.NavBtnService;
import com.ruizhong.PIW.utils.Result;
import com.ruizhong.PIW.utils.ResultCode;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NavBtnServiceImpl extends ServiceImpl<NavBtnMapper, TNavbtn> implements NavBtnService {
    @Override
    public Result getNavBtn(Integer mainId) {
        List<String> mainIds = new ArrayList<>(1);
        mainIds.add(mainId.toString());
        List<TNavbtn> allNavList = new ArrayList<>();
        List<TNavbtn> list = null;
        HashMap<String, TNavbtn> resultMap = new HashMap<>();
        while (!CollectionUtils.isEmpty(mainIds)) {
            list = lambdaQuery()
                    .in(TNavbtn::getMainNav, mainIds)
                    .list();
            allNavList.addAll(list);
            mainIds = list.stream().map(TNavbtn::getId).collect(Collectors.toList());
        }
        for (TNavbtn tNavbtn : allNavList) {
            Integer mainNav = tNavbtn.getMainNav();
            TNavbtn mother = resultMap.get(mainNav.toString());
            if (mother ==null){
                resultMap.put(tNavbtn.getId(),tNavbtn);
                continue;
            }
            List<TNavbtn> sonNavs = mother.getSonNavs();
            if (CollectionUtils.isEmpty(sonNavs)){
                sonNavs=new ArrayList<>();
                mother.setSonNavs(sonNavs);
            }
            sonNavs.add(tNavbtn);
        }
        return new Result(ResultCode.success, null, resultMap);
    }
}
